package com.taxshare.seller.service;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.seller.entity.SellerInvoiceEntity;
import com.taxshare.seller.entity.SellerShangPinEntity;
import com.taxshare.seller.mapper.SellerShangPinMapper;
import com.taxshare.seller.util.LqHttpUtil;
import com.taxshare.tools.token.UserInfoUtil;
import com.taxshare.tools.util.UUIDUitl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 税收分类编码
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public class SellerShangPinService extends ServiceImpl<SellerShangPinMapper, SellerShangPinEntity> {

    @Autowired
    SellerShangPinMapper sellerShangPinMapper; // 假设您有一个 Mapper

    @Value("${lqpt.url}")
    private String url;

    /**
     * 查询税收分类编码信息
     *
     * @param
     * @return
     */
    public Map CXSSFLBM() {
        log.info("========查询税收分类编码信息========开始======");
        Map<String, Object> resultMap = new HashMap();
        JSONObject reqJson = new JSONObject();
        Map<String, Object> param = new HashMap<>();
        param.put("nsrsbh", "91140000731914164T");  //纳税人识别号/统一社会信用代码
        String reqStr = reqJson.toJSONString();
        log.info("========入参======>>>" + reqStr);
        try {
            url = url + "202007/CXSSFLBM";
            LqHttpUtil lqHttpUtil = new LqHttpUtil();
            JSONObject result_json = lqHttpUtil.sendPostLq(reqStr, url);
            // 处理响应
            JSONObject response = result_json.getJSONObject("Response");
            String returncode = result_json.getString("returncode");
            if ("00".equals(returncode)) {
                // 成功处理
                JSONObject data = response.getJSONObject("Data");
                JSONArray ssbmList = data.getJSONArray("ssbmList"); // 获取 ssbmList

                // 循环遍历 ssbmList 并入库
                for (int i = 0; i < ssbmList.size(); i++) {
                    JSONObject item = ssbmList.getJSONObject(i);
                    // 提取需要的字段
                    // 创建 SellerShangPinEntity 实例
                    SellerShangPinEntity entity = new SellerShangPinEntity();
                    entity.setId(UUIDUitl.UUID32(false));
                    entity.setFjbm(item.getString("fjbm"));
                    entity.setSphfwssflhbbm(item.getString("sphfwssflhbbm"));
                    entity.setTdyslxdm(item.getString("tdyslxdm"));
                    entity.setCezs(item.getString("cezs"));
                    entity.setBzs(item.getString("bzs"));
                    entity.setJzjtdm(item.getString("jzjtdm"));
                    entity.setSphfwmc(item.getString("sphfwmc"));
                    entity.setSphfwfljc(item.getString("sphfwfljc"));
                    entity.setSm(item.getString("sm"));
                    entity.setZzssl(item.getString("zzssl"));
                    entity.setZsl(item.getString("zsl"));
                    entity.setZzstsgl(item.getString("zzstsgl"));
                    entity.setZzszcyj(item.getString("zzszcyj"));
                    entity.setZzstsnrdm(item.getString("zzstsnrdm"));
                    entity.setXfsgl(item.getString("xfsgl"));
                    entity.setXfszcyj(item.getString("xfszcyj"));
                    entity.setXfstsnrdm(item.getString("xfstsnrdm"));
                    entity.setGjz(item.getString("gjz"));
                    entity.setSfhzx(item.getString("sfhzx"));
                    entity.setDytjjbm(item.getString("dytjjbm"));
                    entity.setHgjcksppm(item.getString("hgjcksppm"));
                    entity.setQyrq(item.getString("qyrq"));
                    entity.setTyrq(item.getString("tyrq"));
                    entity.setCreateTime(new Date());
                    entity.setCreateUserid(UserInfoUtil.getUserID());

                    // 这里可以调用你的数据库操作方法进行插入
                    baseMapper.insert(entity);
                }
                resultMap.put("code", "0000");
                resultMap.put("msg", "同步成功");
            } else {
                // 失败处理
                JSONObject error = response.getJSONObject("Error");
                String errorCode = error.getString("Code");
                String errorMessage = error.getString("Message");
                resultMap.put("code", errorCode);
                resultMap.put("msg", errorMessage);
            }
        } catch (Exception e) {
            log.error("查询税收分类编码信息,异常：", e);
            resultMap.put("code", "9999");
            resultMap.put("msg", e);
            e.printStackTrace();
        }
        log.info("========查询税收分类编码信息========结束======");
        return resultMap;
    }

    /**
     * 查询全部税收名称
     */
    public List<SellerShangPinEntity> getAllEntities() {
        List<SellerShangPinEntity> entities = sellerShangPinMapper.selectList(null);
        return entities;
    }
}
